package com.lsfb.signln.Location.TaskMsg;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ajguan.library.EasyRefreshLayout;
import com.lsfb.signln.Location.TaskMsg.Leave.LeaveActivity;
import com.lsfb.signln.Location.TaskMsg.MsgDetails.MsgDetailsActivity;
import com.lsfb.signln.Location.TaskMsg.Replenish.ReplenishActivity;
import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.MyDialog;
import com.lsfb.signln.Utils.URLString;
import com.lsfb.signln.View.SignLoadview;
import com.lsfb.signln.View.SignRefreshHeadView;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2018/1/24 0024.
 */
@ContentView(R.layout.aty_task_msg)
public class TaskMsgActivity extends LsfbActivity implements ApprovalListAdapter.ItemClickListener, MessageListAdapter.ItemClickListener {

    /**
     * 审批
     */
    @ViewInject(R.id.msg_text1)
    private TextView tv_approval;

    /**
     * 通知
     */
    @ViewInject(R.id.msg_text2)
    private TextView tv_message;

    /**
     * 审批列表
     */
    @ViewInject(R.id.rv_approval)
    private RecyclerView rv_approval;
    private ApprovalListAdapter adapter_approval;
    private List<ApprovalBean.ListBean> approvals;

    /**
     * 暂无数据
     */
    @ViewInject(R.id.aty_statistics_leave_layout_null)
    private RelativeLayout layout_null;

    /**
     * 下拉刷新 上拉加载
     */
    @ViewInject(R.id.easylayout)
    private EasyRefreshLayout easylayout;

    //    private boolean IsRefresh_approval = true;
//    private boolean IsRefresh_message = true;
    private boolean IsRefresh = true;

    private int page = 0;

    private MyDialog.Builder builder;
    private MyDialog mDialog;

    private int type = 0;

    @Override
    public void initView() {
        builder = new MyDialog.Builder(this);

        //审批
        easylayout.setLoadMoreView(new SignLoadview(this));
        easylayout.setRefreshHeadView(new SignRefreshHeadView(this));
        easylayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
            //加载更多
            @Override
            public void onLoadMore() {
                page++;
                IsRefresh = false;
                getApproval();
            }

            //刷新
            @Override
            public void onRefreshing() {
                page = 0;
                IsRefresh = true;
                getApproval();
            }
        });
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void Resume() {
        page = 0;
        IsRefresh = true;
        getApproval();
//        getMessage();
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_msg_back, R.id.msg_btn1, R.id.msg_btn2, R.id.aty_msg_clear})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.aty_msg_back:
                finish();
                break;
            //审批
            case R.id.msg_btn1:
                tv_approval.setBackground(getResources().getDrawable(R.drawable.frame_down_forgive));
                tv_message.setBackground(getResources().getDrawable(R.drawable.btn_uncheck));

                type = 0;
                page = 0;
                IsRefresh = true;
                getApproval();
                break;
            //通知
            case R.id.msg_btn2:
                tv_approval.setBackground(getResources().getDrawable(R.drawable.btn_uncheck));
                tv_message.setBackground(getResources().getDrawable(R.drawable.frame_down_forgive));

                type = 1;
                page = 0;
                IsRefresh = true;
                getApproval();
                break;
            //清理
            case R.id.aty_msg_clear:
                clear(null, null, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        HashMap hashMap = new HashMap();
                        new BaseInternet().getData_Get(URLString.CLEARMSG, hashMap, String.class, new MessageClearEvent<String>(), true, false);

                        mDialog.dismiss();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                        //取消
                    }
                });
                break;
        }
    }

    @EventAnnotation
    public void eventReuslt(MessageClearEvent<String> event) {
        switch (event.getCode()) {
            case 200:
                T.showShort(this, "消息已清空");
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    public void clear(String confirmText, String cancelText, View.OnClickListener conFirmListener, View.OnClickListener cancelListener) {
        mDialog = builder.setPositiveButton(confirmText, conFirmListener)
                .setNegativeButton(cancelText, cancelListener)
                .setContext("是否删除所有信息")
                .createTwoButtonDialog();
        mDialog.show();
    }

    @Override
    public void initData() {
        approvals = new ArrayList<>();
        adapter_approval = new ApprovalListAdapter(this, approvals, R.layout.item_msg, this);
        rv_approval.setLayoutManager(new LinearLayoutManager(this));
        rv_approval.setAdapter(adapter_approval);

        page = 0;
        IsRefresh = true;
        getApproval();
    }

    /**
     * 获取审批列表
     */
    private void getApproval() {
        HashMap hashMap = new HashMap();
        hashMap.put("page", String.valueOf(page));
        switch (type) {
            case 0:
                new BaseInternet().getData_Get(URLString.GETNEEDME, hashMap, ApprovalBean.class, new ApprovalListEvent<ApprovalBean>(), true, false);
                break;
            case 1:
                new BaseInternet().getData_Get(URLString.GETMSGLIST, hashMap, ApprovalBean.class, new ApprovalListEvent<ApprovalBean>(), true, false);
//                new BaseInternet().getData_Get(URLString.GETMSGLIST, hashMap, MessageBean.class, new MessageListEvent<MessageBean>(), true, false);
                break;
        }
    }

    @EventAnnotation
    public void eventResult(ApprovalListEvent<ApprovalBean> event) {
        easylayout.refreshComplete();
        easylayout.loadMoreComplete();
        switch (event.getCode()) {
            case 200:
                if (event.getData().getList() != null && !event.getData().getList().isEmpty()) {
                    if (IsRefresh) {
                        approvals.clear();
                    }
                    approvals.addAll(event.getData().getList());
                    layout_null.setVisibility(View.GONE);
                } else {
                    if (IsRefresh) {
                        approvals.clear();
                        layout_null.setVisibility(View.VISIBLE);
                    } else {
                        page--;
                        T.showShort(this, "没有更多数据");
                        layout_null.setVisibility(View.GONE);
                    }
                }
                adapter_approval.notifyDataSetChanged();
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    /**
     * 审批Item
     *
     * @param model
     */
    @Override
    public void onItem(ApprovalBean.ListBean model) {
        switch (model.getType()) {
            //跳转请假申请详情
            case 2:
                startActivity(new Intent(TaskMsgActivity.this, LeaveActivity.class).putExtra("lid", String.valueOf(model.getId())));
                break;
            //跳转补录申请详情
            case 1:
                startActivity(new Intent(TaskMsgActivity.this, ReplenishActivity.class).putExtra("rid", String.valueOf(model.getId())));
                break;
            //跳转通知详情
            default:
                startActivity(new Intent(TaskMsgActivity.this, MsgDetailsActivity.class).putExtra("id", String.valueOf(model.getId())));
                break;
        }
    }

    /**
     * 通知Item
     *
     * @param model
     */
    @Override
    public void onMessage(MessageBean.ListBean model) {
        //跳转通知详情
        startActivity(new Intent(TaskMsgActivity.this, MsgDetailsActivity.class).putExtra("id", String.valueOf(model.getId())));
    }
}
